//
//  CredateCooPer.m
//  i5db
//
//  Created by iMuse on 15-5-20.
//  Copyright (c) 2015年 mySelf. All rights reserved.
//

#import "CredateTogrtherController.h"
#import "AppCenter.h"
#import "AppButtonView.h"
#import "APPTextController.h"
#import "UUDatePicker.h"
#import "NotificationNavView.h"
#import "ActivityPalceController.h"

#define hegiht 40

@interface CredateTogrtherController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,onButtonDelegate,textViewDelegate,UUDatePickerDelegate,UITextFieldDelegate>
{
    UIScrollView * scrollow;
    
    UIView      * contentView;
    
    UIButton    * activityNameButton;
    UILabel     * activityLabel;
    UIImageView * imageView;
    
    UIView      * timeView;
    UILabel     * imageLabel;
    
    UITextField    * startTimeField;
    UITextField    * endTimeField;
    UILabel     * line;
    UILabel     * startLabel;
    UILabel     * endLabel;
    
    UIButton    * activityDetailButton;
    UILabel     * activityDetailLabel;
    
    UITextField * currentField;
    
    NSLayoutConstraint * contentHeight;
    NSLayoutConstraint * detailHeight;
}

@property (nonatomic,strong) NSString* activityNameString;
@property (nonatomic,strong) NSString* activityPlaceString;
@property (nonatomic,strong) NSString* activityLimitString;
@property (nonatomic,strong) NSString* activityDetailString;

@end

@implementation CredateTogrtherController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       self.title = @"发布聚会";
        self.activityDetailString = @"";
        self.activityLimitString = @"";
        self.activityDetailString = @"";
        self.activityPlaceString = @"";
       
    }
    return self;
}

-(void) initNavigationBar
{
    [super initNavigationBar];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitle:@"发布" forState:UIControlStateNormal];
    
    /* [button setImage:[UIImage imageNamed:@"edit.png"] forState:UIControlStateNormal];
     [button setImage:[UIImage imageNamed:@"edit.png"] forState:UIControlStateSelected];
     */
    
    [button addTarget:self action:@selector(credateCooPer:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *temporaryBarButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    temporaryBarButtonItem2.style = UIBarButtonItemStylePlain;
    
    self.navigationItem.rightBarButtonItem = temporaryBarButtonItem2;
}

-(void)credateCooPer:(UIButton*)button
{
    
    [self checkEmpty];
    
}

-(BOOL)checkEmpty
{
    if (!self.activityNameString || [self.activityNameString isEqualToString:@""])
    {
        [NotificationNavView showMessage:@"活动名称没有填哦"];
        return NO;
    }
    
    if (!self.activityPlaceString || [self.activityPlaceString isEqualToString:@""])
    {
        [NotificationNavView showMessage:@"活动地点不能不写啊"];
    }
    
    if (!self.activityDetailString || [self.activityDetailString isEqualToString:@""])
    {
        [NotificationNavView showMessage:@"活动详情还没有呢"];
        return NO;
    }
    
    if (!imageView.image)
    {
       [NotificationNavView showMessage:@"请上传活动海报"];
        return NO;
    }
    
    if (!startTimeField.text || [startTimeField.text isEqualToString:@""])
    {
        [NotificationNavView showMessage:@"请填写活动开始时间"];
        
        return NO;
    }
    
    if (!endTimeField.text || [endTimeField.text isEqualToString:@""])
    {
        [NotificationNavView showMessage:@"请填写活动结束时间"];
        
        return NO;
    }
    
    return YES;
}

-(void) updateViewConstraints
{
    [super updateViewConstraints];
    
    [scrollow autoPinToTopLayoutGuideOfViewController:self withInset:0];
    [scrollow autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [scrollow autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [scrollow autoPinToBottomLayoutGuideOfViewController:self withInset:0];
    
    [contentView autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:scrollow withOffset:-64];
    [contentView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:scrollow withOffset:0];
    [contentView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:scrollow withOffset:0];
    [contentView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:scrollow withOffset:0];
    [contentView autoSetDimension:ALDimensionWidth toSize:SCREEN_WIDTH];
    
    
    [activityNameButton autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:15];
    [activityNameButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [activityNameButton autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [activityNameButton autoSetDimension:ALDimensionHeight toSize:hegiht];
    
    [activityLabel autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    
    [timeView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:activityNameButton withOffset:15];
    [timeView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [timeView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [timeView autoSetDimension:ALDimensionHeight toSize:120];
    
    [imageView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [imageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10];
    [imageView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10];
    [imageView autoSetDimension:ALDimensionWidth toSize:80];
    
    [imageLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [imageLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [imageLabel autoSetDimension:ALDimensionHeight toSize:20];
    [imageLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:30];
    
    [startTimeField autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10];
    [startTimeField autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:imageView withOffset:10];
    [startTimeField autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [startTimeField autoSetDimension:ALDimensionHeight toSize:50];
   
    
    [line autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:imageView withOffset:10];
    [line autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [line autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:startTimeField withOffset:0];
    [line autoSetDimension:ALDimensionHeight toSize:0.5];
    
    [endTimeField autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:line withOffset:0];
    [endTimeField autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:imageView withOffset:10];
    [endTimeField autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [endTimeField autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10];
    
    
    
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
     [[IQKeyboardManager sharedManager]setEnable:NO];
    [self.view setNeedsUpdateConstraints];
    [self initNavigationBar];
    [self initView];
   
}

-(void) initView
{
    scrollow = [UIScrollView newAutoLayoutView];
    scrollow.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollow];
    
    contentView = [UIView newAutoLayoutView];
   
    [scrollow addSubview:contentView];
    
    contentHeight =  [contentView autoSetDimension:ALDimensionHeight toSize:SCREEN_HEIGHT-64];
    
    activityNameButton = [UIButton newAutoLayoutView];
    [activityNameButton setBackgroundColor:[UIColor whiteColor]];
    [activityNameButton addTarget:self action:@selector(activityName:) forControlEvents:UIControlEventTouchUpInside];
    activityLabel = [UILabel newAutoLayoutView];
    activityLabel.tag = 1;
    activityLabel.text = @"活动名称(4-25字)";
    activityLabel.textColor = [AppCenter color:@"188,188,188,1"];
    activityLabel.font = [UIFont systemFontOfSize:12];
    [activityNameButton addSubview:activityLabel];
    [contentView addSubview:activityNameButton];
    
    timeView = [UIView newAutoLayoutView];
    timeView.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:timeView];
    
    imageView = [UIImageView newAutoLayoutView];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.layer.borderColor = [AppCenter color:@"188,188,188,1"].CGColor;
    imageView.layer.borderWidth = 0.5;
    imageView.backgroundColor = [UIColor whiteColor];
    imageView.userInteractionEnabled = YES;
    [timeView addSubview:imageView];
    
    UITapGestureRecognizer * gester = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImage:)];
    [imageView addGestureRecognizer:gester];
    
    imageLabel = [UILabel newAutoLayoutView];
    imageLabel.textColor  = [AppCenter color:@"188,188,188,1"];
    imageLabel.text = @"上传海报图片";
    imageLabel.textAlignment = NSTextAlignmentCenter;
    imageLabel.font = [UIFont systemFontOfSize:8];
    [imageView addSubview:imageLabel];
    
    startTimeField = [UITextField newAutoLayoutView];
    startTimeField.placeholder = @"开始时间";
   
    startTimeField.tag = 11;
    startTimeField.delegate = self;
    [timeView addSubview:startTimeField];
    
    
    line = [UILabel newAutoLayoutView];
    line.backgroundColor = [AppCenter color:@"188,188,188,1"];
    [timeView addSubview:line];
    
    endTimeField = [UITextField newAutoLayoutView];
   
    endTimeField.placeholder = @"结束时间";
    endTimeField.tag = 12;
    endTimeField.delegate = self;
    [timeView addSubview:endTimeField];
   
    
    AppButtonView * locationButton = [AppButtonView newAutoLayoutView];
    locationButton.delegate = self;
    locationButton.tag = 2;
    [locationButton setLeftText:@"活动地点"];
    [contentView addSubview:locationButton];
    
    [locationButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [locationButton autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [locationButton autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:timeView withOffset:15];
    [locationButton autoSetDimension:ALDimensionHeight toSize:hegiht];
    
    AppButtonView * limitButton = [AppButtonView newAutoLayoutView];
    limitButton.delegate = self;
    limitButton.tag = 3;
    [limitButton setLeftText:@"人数上限"];
    [limitButton setRightText:@"无上限"];
    [limitButton hiddenLine];
    [contentView addSubview:limitButton];
    
    [limitButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [limitButton autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [limitButton autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:locationButton withOffset:0];
    [limitButton autoSetDimension:ALDimensionHeight toSize:hegiht];
    
    
    activityDetailButton = [UIButton newAutoLayoutView];
    activityDetailButton.backgroundColor = [UIColor whiteColor];
    
    [activityDetailButton addTarget:self action:@selector(detailButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [contentView addSubview:activityDetailButton];
    activityDetailLabel = [UILabel newAutoLayoutView];
    activityDetailLabel.tag = 4;
    activityDetailLabel.numberOfLines = 0;
    activityDetailLabel.text =@"活动介绍(30-2000字)";
   
    activityDetailLabel.font = [UIFont systemFontOfSize:12];
    activityDetailLabel.textColor = [AppCenter color:@"188,188,188,1"];
    [activityDetailButton addSubview:activityDetailLabel];
    
    [activityDetailButton autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:limitButton withOffset:15];
    [activityDetailButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [activityDetailButton autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
  detailHeight =  [activityDetailButton autoSetDimension:ALDimensionHeight toSize:40];
    [activityDetailLabel autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
    
  
 
    
    
}
-(void)selectTime:(UIButton*) button
{
    NSDate *now = [NSDate date];
  
    UUDatePicker *datePicker= [[UUDatePicker alloc]initWithframe:CGRectMake(0, 0, 320, 200)
                                                        Delegate:self
                                                     PickerStyle:UUDateStyle_YearMonthDayHourMinute];
    datePicker.ScrollToDate = now;
    datePicker.maxLimitDate = now;
    datePicker.minLimitDate = [now dateByAddingTimeInterval:-111111111];
   
}

-(void)selectImage:(UITapGestureRecognizer*)gester
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:nil];
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:NULL];
    imageView.image = chosenImage;
    
    imageLabel.text = @"";
    
}

-(void)detailButton:(UIButton*) button
{
    APPTextController * textController = [[APPTextController alloc] initWith:2000 minNumber:30 tag:@"4" title:@"活动详情" text:self.activityDetailString];
  
    textController.delegate = self;
    [self.navigationController pushViewController:textController animated:YES];
}

-(void)activityName:(UIButton*) button
{
   
    APPTextController * textController = [[APPTextController alloc] initWith:25 minNumber:4 tag:@"1" title:@"活动名称" text: self.activityNameString];
    textController.delegate = self;
    [self.navigationController pushViewController:textController animated:YES];
    
}
-(void)textViewString: (NSString*) tag text:(NSString*) text
{
    NSInteger myTag = [tag integerValue];
    if (myTag==1)
    {
        activityLabel.text = text;
        activityLabel.textColor = [AppCenter color:@"126,126,126,1"];
        self.activityNameString = text;
        return;
    }
    if (myTag ==2)
    {
        self.activityPlaceString = text;
       
    }
    
    if (myTag == 3)
    {
        self.activityLimitString = text;
    }
    
    if (myTag == 4)
    {
        
        activityDetailLabel.text = text;
        activityDetailLabel.textColor = [AppCenter color:@"126,126,126,1"];
        CGSize size = [AppCenter sizeWithString:activityDetailLabel.text font:activityDetailLabel.font maxHeight:1000 maxWeight:SCREEN_WIDTH];
        
        if (size.height<40)
        {
            size.height = 40;
        }
        detailHeight.constant =size.height;
        
        contentHeight.constant = SCREEN_HEIGHT-64+detailHeight.constant;
        
        self.activityDetailString = text;
        return;
    }
    
    AppButtonView * button = (AppButtonView*)[self.view viewWithTag:myTag];
    [button setRightText:text];
}


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    currentField = textField;
     NSDate *now = [NSDate date];
  
    UUDatePicker *datePicker= [[UUDatePicker alloc]initWithframe:CGRectMake(0, 0,SCREEN_WIDTH, 200)
                                                        Delegate:self
                                                     PickerStyle:UUDateStyle_YearMonthDayHourMinute];
    datePicker.ScrollToDate = now;
    currentField.inputView = datePicker;
    
}

-(void)uuDatePicker:(UUDatePicker *)datePicker year:(NSString *)year month:(NSString *)month day:(NSString *)day hour:(NSString *)hour minute:(NSString *)minute weekDay:(NSString *)weekDay
{
    if (currentField.tag==11)
    {
        startTimeField.text = [NSString stringWithFormat:@"%@/%@/%@ %@:00:00",year,month,day,hour];
        
        
    }else if (currentField.tag==12)
    {
        endTimeField.text = [NSString stringWithFormat:@"%@/%@/%@ %@:00:00",year,month,day,hour];

    }
    
}
-(void)onTapButton:(NSMutableDictionary*)_dic
{
    NSInteger myTag = [[_dic valueForKey:@"tag"] integerValue];
    
    if(myTag ==2)
    {
        ActivityPalceController * ActivityController = [[ActivityPalceController alloc] init];
        [self.navigationController pushViewController:ActivityController animated:YES];
    }else
    {
    APPTextController * textController = [[APPTextController alloc] initWith:25 minNumber:4 tag:[_dic valueForKey:@"tag"] title:[_dic valueForKey:@"left"] text:[_dic valueForKey:@"right"]];
    [textController hiddent];
    textController.delegate = self;
    [self.navigationController pushViewController:textController animated:YES];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


- (void)viewWillAppear:(BOOL)animated {
    
        [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES animated:NO];

    
    
}


- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [[self rdv_tabBarController] setTabBarHidden:NO animated:NO];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
